package com.example.dormmanage.handler;

import com.example.dormmanage.util.CustomerException;
import com.example.dormmanage.view.ResultView;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 全局异常处理器
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class,Service.class})
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 异常处理方法
     *
     * @return
     */
//    @ExceptionHandler(SQLException.class)
//    public R<String> exceptionHandler(SQLException exception) {
//        log.error(exception.getMessage());
//        System.out.println(exception.getMessage());
//        if (exception.getMessage().contains("Duplicate entry")) {
//            String[] split = exception.getMessage().split(" ");
//            String msg = split[2] + "已存在";
//            return R.error(msg);
//        }
//        return R.error("未知错误");
//    }

    @ExceptionHandler(CustomerException.class)
    public ResultView<String> exceptionHandler(CustomerException exception) {
        log.error(exception.getMessage());
        return ResultView.fail(exception.getMessage());
    }
}
